package com.csair.seam.controller.pc;

import com.csair.seam.domain.model.entity.IdleReceiveExecl;
import com.csair.seam.domain.model.enums.VisitsModule;
import com.csair.seam.domain.model.param.IdleReceiveParam;
import com.csair.seam.domain.service.IEquipOrgService;
import com.csair.seam.infrastructure.log.LogOperation;
import com.csair.seam.infrastructure.log.VisitsLog;
import com.csair.seam.infrastructure.poi.entity.ExcelImportResult;
import com.csair.seam.infrastructure.poi.exception.ExcelException;
import com.csair.seam.infrastructure.poi.utils.ExcelPoiUtil;
import com.csair.seam.infrastructure.web.BaseController;
import com.csair.seam.infrastructure.web.BaseResponse;
import com.csair.seam.infrastructure.web.ResponseCode;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.csair.seam.domain.model.entity.IdleReceive;
import com.csair.seam.domain.service.IdleReceiveService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@Api(tags = "闲置资产接口")
@RestController
@RequestMapping("/pc/idleReceive")
public class IdleReceiveController extends BaseController {

	@Autowired
	private IdleReceiveService idleReceiveService;

	@Autowired
	private IEquipOrgService iEquipOrgService;

	@ApiOperation(value = "闲置资产(分页)", notes = "page",httpMethod="POST")
	@LogOperation(bizName = "闲置资产条件分页查询")
	@PostMapping("/page")
	@VisitsLog(module = VisitsModule.IDLE_RECEIVE_QUERY)
	public BaseResponse<PageInfo<IdleReceive>> page(@ApiParam(value = "查询参数") IdleReceiveParam param) {
		return buildSuccess(idleReceiveService.getIdleReceivePage(param));
	}

	@ApiOperation(value = "年度成交金额", notes = "getYearInsum",httpMethod="GET")
	@LogOperation(bizName = "年度成交金额")
	@GetMapping("/getYearInsum")
	public BaseResponse<BigDecimal> getYearInsum(){
		return buildSuccess(idleReceiveService.getYearInsum());
	}


	@ApiOperation(value = "年度成交量", notes = "getYearReceivnum",httpMethod="GET")
	@LogOperation(bizName = "年度成交量")
	@GetMapping("/getYearReceivnum")
	public BaseResponse<Integer> getYearReceivnum(){
		return buildSuccess(idleReceiveService.getYearReceivnum());
	}


	@ApiOperation(value = "累计成交金额", notes = "getAllInsum",httpMethod="GET")
	@LogOperation(bizName = "累计成交金额")
	@GetMapping("/getAllInsum")
	public BaseResponse<BigDecimal> getAllInsum(){
		return buildSuccess(idleReceiveService.getAllInsum());
	}


	@ApiOperation(value = "累计成交金额", notes = "getAllReceivnum",httpMethod="GET")
	@LogOperation(bizName = "累计成交金额")
	@GetMapping("/getAllReceivnum")
	public BaseResponse<Integer> getAllReceivnum(){
		return buildSuccess(idleReceiveService.getAllReceivnum());
	}

	@ApiOperation(value = "发布公司成交量和成交金额", notes = "getOutcorpCountAndInsum",httpMethod="GET")
	@LogOperation(bizName = "发布公司成交量和成交金额")
	@GetMapping("/getOutcorpCountAndInsum")
	public BaseResponse<Object> getOutcorpCountAndInsum(String startDate,
														String endDate,
														String intype,
														String category){
		return buildSuccess(idleReceiveService.getOutcorpCountAndInsum(startDate,endDate,intype,category));
	}

	@ApiOperation(value = "接收公司成交量和成交金额", notes = "getIncorpCountAndInsum",httpMethod="GET")
	@LogOperation(bizName = "接收公司成交量和成交金额")
	@GetMapping("/getIncorpCountAndInsum")
	public BaseResponse<Object> getIncorpCountAndInsum(String startDate,
													   String endDate,
													   String intype,
													   String category){
		return buildSuccess(idleReceiveService.getIncorpCountAndInsum(startDate,endDate,intype,category));
	}

	@GetMapping("/idle/exportData")
	@ApiOperation(value = "闲置资产导出 ", notes = "exportData",httpMethod="GET")
	@LogOperation(bizName = "闲置资产导出")
	@VisitsLog(module = VisitsModule.IDLE_RECEIVE_EXPORT)
	public Object exportData(HttpServletRequest request,HttpServletResponse response,IdleReceiveParam param){
		List<IdleReceiveExecl> irs = idleReceiveService.getIdleReceive(param);
		if(CollectionUtils.isEmpty(irs)) {
			return new BaseResponse<>(ResponseCode.ERROR, "查询的结果集为空，导出失败",null);
		}
		ExcelPoiUtil.exportExcel("闲置资产报表明细.xlsx", irs, null, request, response);
		return buildSuccess(null);
	}

	@PostMapping(value = "/idle/importExcel",headers = "content-type=multipart/form-data")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file",value = "单个文件，",paramType = "formData",required = true,dataType = "file")
	})
	@ApiOperation(value = "闲置资产导入 ", notes = "importExcel",httpMethod="POST")
	@LogOperation(bizName = "闲置资产导入")
	@VisitsLog(module = VisitsModule.IDLE_RECEIVE_IMPORT)
	public Object importExcel(@RequestParam(value = "file", required = true) MultipartFile file){
		try {
			ExcelImportResult<IdleReceive> execlResult = ExcelPoiUtil.importExcel(file,IdleReceive.class);
			if(StringUtils.isNotEmpty(execlResult.getErrorMsg()))
				return buildError(execlResult.getErrorMsg(),null);
			List<IdleReceive> ids = execlResult.getSuccessList();
			Map<String,String> errorMessage = new HashMap<>();
			for(IdleReceive ir : ids){
				ir.setCreator(getAccount());
				ir.setCreaTime(new Date());
				ir.setShowId(1);

				errorMessage = ir.checkIdle(iEquipOrgService,idleReceiveService,ir);
				if(!errorMessage.isEmpty()) return buildError(errorMessage.toString(),null);
				//idleReceiveService.checkIdle(ir,errorMessage);
				//if(!errorMessage.isEmpty()) return buildError(errorMessage.toString(),null);
			}
			idleReceiveService.addBatchIdleReceive(ids);
		}catch (ExcelException e){
			e.printStackTrace();
			return buildError(e.getMessage(),null);
		}
		return buildSuccess(null);
	}
}
